package com.example.mylayoutapp;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
//	@Override
//	protected void onResume() {
//		if(thread == null)
//			return;
//		
//		thread.resume();
//	}

	private final int REQUEST_CODE_ACTIVITY_ONE = 1;
	private final int REQUEST_CODE_ACTIVITY_TWO = 2;
	private Thread thread;
	private Handler handler;	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		thread = new Thread() {
			public void run() {
				try {
					while (true) {
						Thread.sleep(1000);					
						SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
						Log.v("A", String.format("Current Date %s", sdf.toString()));
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		thread.setName("myThread");
		thread.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onOpenActivityOne(View view) {
		Toast.makeText(this, "Open Activity One", Toast.LENGTH_SHORT).show();

		Intent intent = new Intent(this, ActivityOne.class);
		startActivityForResult(intent, REQUEST_CODE_ACTIVITY_ONE);
	}

	public void onOpenActivityTwo(View view) {
		Toast.makeText(this, "Open Activity Two", Toast.LENGTH_SHORT).show();

		Intent intent = new Intent(this, ActivityTwo.class);
		startActivityForResult(intent, REQUEST_CODE_ACTIVITY_TWO);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		String result = null;

		switch (requestCode) {
		case REQUEST_CODE_ACTIVITY_ONE:
		case REQUEST_CODE_ACTIVITY_TWO:
			if (resultCode == RESULT_OK) {
				result = (String) (data.getExtras().get("myResult"));
				((TextView) this.findViewById(R.id.resultText)).setText(result);
			}
			break;
		}
	}
}
